/* 
    反转字符串
*/

{
  const reverseString = (str) => {
    return str.split("").reverse().join("");
  };
  const str = "chenjiang";
  console.log(reverseString(str));
}

{
  const reverseString = (str) => {
    // 从后遍历，依次存放字符
    let result = "";
    for (let i = str.length - 1; i >= 0; i--) {
      result += str.charAt(i);
    }
    return result;
  };

  const str = "abcdefg";
  console.log(reverseString(str));
}

{
  /* 
    使用递归方式，每调用一次存放一位
  */
  const reverseString = (strIn, position, strOut) => {
    if (position < 0) return strOut;
    strOut += strIn.charAt(position);
    position--;
    return reverseString(strIn, position, strOut);
  };

  const str = "qwerdf";
  const position = str.length;
  const result = "";
  console.log(reverseString(str, position, result));
}
